package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.entity.Encyclo;
import cn.b.training.baby_education_training.entity.EncycloCollection;
import cn.b.training.baby_education_training.entity.UserInfo;
import cn.b.training.baby_education_training.repository.EncycloCollectionRepository;
import cn.b.training.baby_education_training.repository.EncycloRepository;
import cn.b.training.baby_education_training.repository.UserInfoRepository;
import cn.b.training.baby_education_training.service.EncycloCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-04 21:28
 **/
@Service
public class EncycloCollectionServiceImpl implements EncycloCollectionService {
    @Autowired
    private EncycloCollectionRepository encycloCollectionRepository;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private EncycloRepository encycloRepository;
    @Override
    public boolean addEncycloCollection(EncycloCollection encycloCollection) throws Exception {
        UserInfo userInfo = userInfoRepository.getByUserId(encycloCollection.getUserId());
        Encyclo encyclo = encycloRepository.getByEncycloId(encycloCollection.getEncycloId());
        if (userInfo!=null&&encyclo!=null){
            encycloCollection.setUserId(userInfo.getId());
            encycloCollection.setEncycloId(encyclo.getId());
            encycloCollection = encycloCollectionRepository.save(encycloCollection);
        }else {
            throw new Exception("信息不存在");
        }
        if (encycloCollection!=null){
            return true;
        }
        return false;
    }

    @Override
    public List<EncycloCollection> getEncycloCollectionByUserId(Long userId) {
        List<EncycloCollection> collections = encycloCollectionRepository.getEncycloCollectionByUserId(userId);
        return collections;
    }

    @Override
    public Boolean deleteEncycloCollectionById(Long id) {
        try{
            this.encycloCollectionRepository.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
